﻿using System;
using System.Collections.Generic;

namespace TC
{
    public abstract class Dispatcher
    {
        public List<FlowNode> callstack = new List<FlowNode>();

        public Action<int> onComplete;

        public virtual void OnNodeChange(FlowNode node) { }
        public virtual void Destroy() { }
        public virtual void Update(float delta) { }
        public virtual void Call(FlowNode node) { }
        public virtual void Call(FlowPort p) { }
        public virtual void End(int status) { }

        protected void push(FlowNode node)
        {
            callstack.Add(node);
        }

        protected FlowNode pop(int index)
        {
            FlowNode tmp = null;
            int j;
            while (callstack.Count >= index)
            {
                j = callstack.Count - 1;
                tmp = callstack[j];
                callstack.RemoveAt(j);

                //有些是回退节点
                if (tmp is IBack)
                {
                    (tmp as IBack).Back();
                }
                else
                {
                    tmp.Stop();
                }
            }
            return tmp;
        }

        protected FlowNode peek()
        {
            if (callstack.Count > 0)
            {
                return callstack[callstack.Count - 1];
            }
            return null;
        }
    }
}
